import moment from './moment.js'

export default {

	/**
	 * 获取当前月
	 */
	getCurrentMonth() {
		const month = moment().format('MM月');
		const year = moment().format('YYYY');
		let dateObj = {
			month: month,
			year: year
		}
		return dateObj;
	},

	/**
	 * 获取本周周一和周日日期
	 */
	getCurrentWeek() {
		const start = moment().weekday(1).format('YYYY-MM-DD'); //本周一
		const end = moment().weekday(7).format('YYYY-MM-DD'); //本周日
		return [start, end]
	},
	/**
	 * 获取前 i 周的周一和周日日期，并以数组的方式返回。
	 */
	getLastWeek(i) {
		let weekOfDay = parseInt(moment().format('E')); //计算今天是这周第几天
		let last_monday = moment().subtract(weekOfDay + 7 * i - 1, 'days').format('YYYY-MM-DD'); //周一日期
		let last_sunday = moment().subtract(weekOfDay + 7 * (i - 1), 'days').format('YYYY-MM-DD'); //周日日期
		return [last_monday, last_sunday]
	},

	/**
	 * 获取后 i 周的周一和周日日期，并以数组的方式返回。
	 */
	getNextWeek(i) {
		let weekOfDay = parseInt(moment().format('E')); //计算今天是这周第几天
		let next_monday = moment().add((7 - weekOfDay) + 7 * (i - 1) + 1, 'days').format('YYYY-MM-DD'); //周一日期
		let next_sunday = moment().add((7 - weekOfDay) + 7 * i, 'days').format('YYYY-MM-DD'); //周日日期
		return [next_monday, next_sunday]
	},

	/**
	 * 获取两个日期直接的所有日期,并以数组的方式返回。
	 */
	enumerateDaysBetweenDates(startDate, endDate) {
		let dateList = [];
		let SDate = moment(startDate);
		let EDate = moment(endDate);
		let xt;
		// 第一天
		let firstObj = {
			day: SDate.format("DD"), //日期中的日,
			time: SDate.format("YYYY-MM-DD"), //YYYY-MM-DD,
			week: this.getWeek(SDate), //星期
			timestamp: SDate.startOf('day').valueOf(), //时间戳
			isToday: moment(SDate).isSame(moment(), "day"), //是否今日
			dateOrders: [], //存放当日排班订单或者调拨数据
			type: 0, // 当期的排班类型
			...this.getLegalHoliday(SDate),
		}
		dateList.push(firstObj);
		// 中间日期
		while (SDate.add(1, "days").isBefore(EDate)) { // 注意这里add方法处理后SDate对象已经改变。  
			let otherObj = {
				day: SDate.format("DD"), //日期中的日,
				time: SDate.format("YYYY-MM-DD"), //YYYY-MM-DD,
				week: this.getWeek(SDate), //星期,
				timestamp: SDate.startOf('day').valueOf(), //时间戳
				isToday: moment(SDate).isSame(moment(), "day"), //是否今日
				dateOrders: [], //存放当日排班订单或者调拨数据
				type: 0, // 当期的排班类型
				...this.getLegalHoliday(SDate),
			}
			dateList.push(otherObj);
		}
		//最后一天
		let lastObj = {
			day: EDate.format("DD"), //日期中的日,
			time: EDate.format("YYYY-MM-DD"), //YYYY-MM-DD,
			week: this.getWeek(EDate), //星期
			timestamp: EDate.startOf('day').valueOf(), //时间戳
			isToday: moment(EDate).isSame(moment(), "day"), //是否今日
			dateOrders: [], //存放当日排班订单或者调拨数据
			type: 0, // 当期的排班类型
			...this.getLegalHoliday(EDate),
		}
		dateList.push(lastObj);
		return dateList;
	},

	/**
	 * 获取某个日期是周几
	 */
	getWeek(date) { // 参数时间戳
		let week = moment(date).day()
		switch (week) {
			case 1:
				return '周一'
			case 2:
				return '周二'
			case 3:
				return '周三'
			case 4:
				return '周四'
			case 5:
				return '周五'
			case 6:
				return '周六'
			case 0:
				return '周日'
		}
	},
	/*
	 * 根据指定日期检测该日期是否是法定节假日
	 */
	getLegalHoliday(date) {
		//法定节假日每年不一样需根据每年国务院公布的数据进行手动更新时间

		let holidayInfos = [{
			holidayStartTime: '2023-12-30',
			holidayEndTime: '2024-01-01',
			isHoliday: true,
			holidayName: '元旦'
		}, {
			holidayStartTime: '2024-02-10',
			holidayEndTime: '2024-02-17',
			isHoliday: true,
			holidayName: '春节'
		}, {
			holidayStartTime: '2024-04-04',
			holidayEndTime: '2024-04-06',
			isHoliday: true,
			holidayName: '清明节'
		}, {
			holidayStartTime: '2024-05-01',
			holidayEndTime: '2024-05-05',
			isHoliday: true,
			holidayName: '劳动节'
		}, {
			holidayStartTime: '2024-06-08',
			holidayEndTime: '2024-06-10',
			isHoliday: true,
			holidayName: '端午节'
		}, {
			holidayStartTime: '2024-09-15',
			holidayEndTime: '2024-09-17',
			isHoliday: true,
			holidayName: '中秋节'
		}, {
			holidayStartTime: '2024-10-01',
			holidayEndTime: '2024-10-07',
			isHoliday: true,
			holidayName: '国庆节'
		}]

		let legalHolidayInfo = {
			holidayName: '',
			isHoliday: false,
		}
		const currentDateFormat = date.format('YYYY-MM-DD');
		const currentDate = new Date(currentDateFormat);
		for (let holidayItem of holidayInfos) {
			const startDate = new Date(holidayItem.holidayStartTime);
			const endDate = new Date(holidayItem.holidayEndTime);
			if (currentDate >= startDate && currentDate <= endDate) {
				legalHolidayInfo.holidayName = holidayItem.holidayName;
				legalHolidayInfo.isHoliday = true;
			}
		}
		return legalHolidayInfo;
	}
}